/*************************************************************************
> File Name: DataMgr.cpp
> Author: Wu Junqiang
> Mail: 15769154659@163.com
> Created Time: 2018年01月23日 星期二 15时06分18秒
> Note: 
************************************************************************/

#include "DataMgr.h"



DataMgr * DataMgr::data = new DataMgr();

DataMgr * DataMgr::instance()
{
	return data;
}


void DataMgr::connect()
{
	mysql_init(&conn);
    mysql_options(&conn,MYSQL_SET_CHARSET_NAME,"gbk");
	try
	{
		if(mysql_real_connect(&conn, "127.0.0.1", "root", "chunhengkeji", "chunheng", 3306, NULL, 0))
		//if (connRes == NULL)
		{
			cout << "Link successful " << endl;
     			//cout << connRes << endl;
		}
	}
	catch (const std::exception&)
	{

	}
}

bool DataMgr::canRegister(string Name)
{
	MYSQL_RES *res_ptr;
	MYSQL_FIELD *fd;
	MYSQL_ROW sqlrow;
	char flds[10][10];
	int res,i = 0,j ;
	try
	{
		res = mysql_query(&conn,"select Name from ch_user");
		if(res)
		{
			cout << "statement exec failed" << endl;
			mysql_close(&conn);
		}
		else
		{
			res_ptr =   mysql_store_result(&conn);
			if(res_ptr)
			{
			//	cout << "received " << (unsigned long)mysql_num_rows(res_ptr) <<" rows" << endl;
				j = mysql_num_fields(res_ptr);
				while((sqlrow = mysql_fetch_row(res_ptr)))
				{
					for(i = 0 ; i < j ; i ++)
					{
						if(sqlrow[i] == Name)
						{
							cout << "have this user!!!" << endl;
							return true;
						}
					}
				}
			}
			mysql_free_result(res_ptr);
		}
	}
	catch(const std::exception&)
	{
	}
	return false;
}


bool DataMgr::Register(string Name, string Pw)
{
	
	int res;
	try
	{
		string str = "INSERT INTO ch_user (Name,Password) VALUES ('" + Name + "','" + Pw + "')";

		res = mysql_query(&conn,str.c_str());
		if(!res)
		{
			cout << "Inserted " << (unsigned long)mysql_affected_rows(&conn) <<" rows" << endl;
			return true;
		}
		else
		{
			fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&conn),mysql_error(&conn));
		}
	
		mysql_close(&conn);
	}
	catch(const std::exception&)
	{

	}
	return false;
}

//error function
bool DataMgr::CheckPassword(string Name, string Pwd)
{
	string str = "select Name,password from ch_user where Name='" + Name +"' and Password='" + Pwd +"'";
	MYSQL_RES *res_ptr;
	MYSQL_ROW sqlrow;
	char flds[10][10];
	int res,i = 0,j ;
	try
	{
		res = mysql_query(&conn,str.c_str());
		if(res)
		{
			cout << "statement exec failed" << endl;
			mysql_close(&conn);
		}
		else
		{
			res_ptr =   mysql_store_result(&conn);
			if(res_ptr)
			{
				cout << "received " << (unsigned long)mysql_num_rows(res_ptr) <<" rows" << endl;

				sqlrow = mysql_fetch_row(res_ptr);
				if(sqlrow == 0)
				{
					 cout << "CheckPassword is error!!" << endl;
					 return false;
				}
				else
				{
					cout << "successful " << endl;
					return true;
				}
			}
			mysql_free_result(res_ptr);
		}
	}
	catch(const std::exception&)
	{
	}
	return false;
}

bool DataMgr::SaveuserSportDate(SportData data)
{
	int res;
	try
	{
	
		string str ="insert into ch_sport_data (UserID,SportClassID,MachineID,SportTime,ch_sport_data.Group,GroupNum,Isstandard,Info) values((select ID from ch_user where Name='" + data.name + "'),(select ID from ch_sport_class where SportType='" + data.sportClass + "'),(select MachineNum from ch_machine_info where mac = '"+ data.mac +"'),'" + data.sportTime + "','" + data.group + "','" + data.groupNum +"','" + data.isStandard + "','" + data.info + "')";
		cout << str << endl;
		res = mysql_query(&conn,str.c_str());
		if(!res)
		{
			cout << "Inserted " << (unsigned long)mysql_affected_rows(&conn) <<" rows" << endl;
			return true;
		}
		else
		{
			fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&conn),mysql_error(&conn));
		}
	
		mysql_close(&conn);
	}
	catch(const std::exception&)
	{

	}
	return false;
}

list<SportData> DataMgr::QueryuserSportData(string name)
{

	MYSQL_RES *res_ptr;
	MYSQL_ROW sqlrow;
	list<SportData> dd;
	SportData d;
	int res,j,i;

	if(name.empty())
	{
		cout << "Invalid name !!!" << endl;
		return dd ;
	}
	string str ="select SportTime,ch_sport_data.Group,GroupNum,Isstandard,Info from ch_sport_data where UserID = (select ID from ch_user where name = '" + name + "')";

	try
	{
		res = mysql_query(&conn,str.c_str());
		if(res)
		{
			cout << "query failed " << mysql_errno(&conn) << endl;
			mysql_close(&conn);
		}
		else
		{
			//query result
			res_ptr = mysql_store_result(&conn);
			if(res_ptr)
			{
				j = mysql_num_fields(res_ptr);
				while((sqlrow = mysql_fetch_row(res_ptr)))
				{
					for(i = 0 ; i < j; i++)
					{
						switch(i)
						{
							case 0:
                                if(sqlrow[0] == NULL)
                                {
                                    d.sportTime = "";
                                }
                                else
                                {
							         d.sportTime = sqlrow[0];
                                }
								continue;
							case 1:
                                if(sqlrow[1] == NULL)
                                {
                                    d.group = "";
                                }
                                else
                                {
								    d.group = sqlrow[1];
                                }
								continue;
							case 2:
                                if(sqlrow[2] == NULL)
                                {
                                    d.groupNum = sqlrow[2];
                                }
                                else
                                {
								    d.groupNum = sqlrow[2];
                                }
                                continue;
							case 3:
                                if(sqlrow[3] == NULL)
                                {
                                    d.isStandard = "";
                                }
                                else
                                {
								    d.isStandard = sqlrow[3];
                                }
                                continue;
							case 4:
                                if(sqlrow[4] == NULL)
                                {
                                    d.info = "";
                                }
                                else
                                {
								    d.info = sqlrow[4];
                                }
                                continue;
						}
//						cout << sqlrow[i] << "\t";
					}
					cout << "J: " << j << endl;
					d.name = name;
					d.sportClass = "";
                    d.mac = "";
					dd.push_back(d);
				}
				cout << "dd Size: " << dd.size() << endl;
				return dd;
			}
		}

	}
	catch(const std::exception)
	{
	}
}

Person DataMgr::QueryUsrData(string name)
{
	MYSQL_RES *res_ptr;
	MYSQL_ROW sqlrow;
	Person person;
	int res,j,i;

	if(name.empty())
	{
		cout << "Invalid name !!!" << endl;
		return person;
	}
	string str ="select Name,Age,Sex,Height,Weight,ContactInfo from ch_user where name='" + name + "'";

	try
	{
        res =  mysql_query(&conn, str.c_str());
		if(res)
		{
			cout << "query failed " << mysql_errno(&conn) << endl;
			mysql_close(&conn);
		}
		else
		{
			//query result
			res_ptr = mysql_store_result(&conn);
			if(res_ptr)
			{
				j = mysql_num_fields(res_ptr);
				while((sqlrow = mysql_fetch_row(res_ptr)))
				{
					for(i = 0 ; i < j; i++)
					{
						switch(i)
						{
							case 0:
                                if(sqlrow[0] == NULL)
                                {
                                    person.name = "";
                                }
                                else
                                {
								    person.name = sqlrow[0];
                                }
                              //  person.name = sqlrow[0];
								continue;
							case 1:
                                if(sqlrow[1] == NULL)
                                {
                                    person.age = "";
                                }
                                else{
                                    person.age = sqlrow[1];
                                }
							   // person.age = sqlrow[1];
                                continue;
							case 2:
                                if(sqlrow[2] == NULL)
                                {
                                    person.sex = "";
                                }
                                else{
                                    person.sex = sqlrow[2];
                                }
							//	person.sex = sqlrow[2];
								continue;
							case 3:
                                if(sqlrow[3] == NULL)
                                {
                                    person.height = "";
                                }
                                else{
                                    person.height = sqlrow[3];
                                }
							//	person.height = sqlrow[3];
								continue;
							case 4:
                                if(sqlrow[4] == NULL)
                                {
                                    person.weight = "";
                                }
                                else{
                                    person.weight = sqlrow[4];
                                }
						 //		person.weight = sqlrow[4];
								continue;
                            case 5:
                                if(sqlrow[5] == NULL)
                                {
                                    person.contactInfo = "";
                                }else{
                                    person.contactInfo = sqlrow[5];
                                }
                            //    person.contactInfo = sqlrow[5];
                                continue;
						}
					}
                    person.passWord = "";
                    person.datetime = "";
				}
                return person;
			}
		}
	}
	catch(const std::exception)
	{
	}
}
